package zhang.automobileservice;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.json.JSONException;
import org.json.JSONObject;

import fragment.MyselfFragment;
import utils.MyUrl;
import utils.RequestQueueUtils;

public class MyLoginActivity extends FarterActivity implements View.OnClickListener {

    private EditText et_phonenum;
    private EditText et_password;
    private Button tv_forgetpassword;
    private Button tv_instantregister;
    private Button bt_login;
    private String Login_id=null;
    SharedPreferences preferences;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_login);
        initView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.et_phonenum:
                break;
            case R.id.et_password:
                break;
            case R.id.tv_forgetpassword:
                final Intent intent = new Intent(this,FindPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_instantregister:
                Intent intent1 = new Intent(this,RegisterActivity.class);
                startActivity(intent1);
                break;
            case R.id.bt_login:
                String url = MyUrl.Font_Url+MyUrl.login+"phone="+et_phonenum.getText().toString()+"&password="+et_password.getText().toString();
                RequestQueueUtils.exectueJsonRequest(this, url, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        JSONObject obj;
                        try {
                            obj = jsonObject.getJSONObject("data");
                            if("0".equals(jsonObject.getString("code"))){
                                Login_id = obj.getString("id");

                                //SaveUser();
                                preferences=getApplicationContext().getSharedPreferences("TAGtext",MODE_PRIVATE);
                                editor=preferences.edit().putString("id",Login_id);
                                editor.commit();
                            }else{
                                Toast.makeText(MyLoginActivity.this,"登录失败",Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Intent intent2 = new Intent(MyLoginActivity.this, MyselfFragment.class);
                        intent2.putExtra("id",Login_id);
                        startActivity(intent2);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_LONG).show();
                    }
                });
                break;
        }
    }

   /* private void SaveUser() {
        String url1 = MyUrl.Font_Url+MyUrl.getData+"id="+Login_id;
        RequestQueueUtils.exectueJsonRequest(this, url1, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("---","22222222");
                JSONObject obj;
                try {
                    obj = jsonObject.getJSONObject("data");
                    if("0".equals(jsonObject.getString("code"))){
                        String Login_phone = obj.getString("phone");
                        String Login_password = obj.getString("password");
                        String Login_username = obj.getString("userName");
                        String Login_headPhoto = obj.getString("headPic");
                       *//* LoginBean loginBean = new LoginBean(Login_id,Login_phone,Login_password,Login_username,Login_headPhoto);
                        //CarServerLoginDB.getInstance(getApplicationContext()).saveUser(loginBean);
                        CarServerLoginDB db = new CarServerLoginDB(getApplicationContext());
                        db.saveUser(loginBean);
                        Log.e("---",loginBean.toString());*//*
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.e("---","数据请求完成");
                Intent intent2 = new Intent(MyLoginActivity.this, MyselfFragment.class);
                intent2.putExtra("id",Login_id);
                startActivity(intent2);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("-----","数据库");
            }
        });

    }*/

    private void initView() {
        et_phonenum = (EditText) findViewById(R.id.et_phonenum);
        et_phonenum.setOnClickListener(this);
        et_password = (EditText) findViewById(R.id.et_password);
        et_password.setOnClickListener(this);
        tv_forgetpassword = (Button) findViewById(R.id.tv_forgetpassword);
        tv_forgetpassword.setOnClickListener(this);
        tv_instantregister = (Button) findViewById(R.id.tv_instantregister);
        tv_instantregister.setOnClickListener(this);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);
    }

}
